﻿using ArtMath.Component.Annotation;
using ArtMath.Component.CoordinateSystem;
using ArtMath.Control;
using ArtMath.Core.Helper;
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace ArtMath.Editor
{
    [Serializable]
    public class SliderEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context?.Instance != null)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context?.Instance != null && provider != null)
            {
                IWindowsFormsEditorService es = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (es != null)
                {
                    SliderControl editor;
                    Array arr = context.Instance as Array;
                    if (arr != null)//数组一定是选择了多个画笔（此分支存在BUG，不知如何批量赋值给选定的对象）
                    {
                        //double[] val = new double[arr.Length];
                        //PenBase pen = arr.GetValue(0) as PenBase;
                        //if (pen != null)
                        //{
                        //    bool isHL = pen is Highlighter;
                        //    for (int i = 0; i < val.Length; i++)
                        //    {
                        //        val[i] = (arr.GetValue(i) as PenBase).PenWidth.Value;
                        //    }
                        //    editor = new SliderControl(Utility.Same(val) ? (int)val[0] : (isHL ? 30 : 3),
                        //        isHL ? 10 : 1, isHL ? 500 : 20, isHL ? 30 : 3);
                        //    editor.RefreshObject += (int newValue) =>
                        //    {

                        //    };
                        //}
                    }
                    else
                    {
                        PenBase pen = context.Instance as PenBase;
                        if (pen != null)
                        {
                            bool isHL = pen is Highlighter;
                            editor = new SliderControl((int)pen.PenWidth.Value, isHL ? 10 : 1, isHL ? 500 : 20, isHL ? 30 : 3);
                            editor.RefreshObject += (int newValue) =>
                            {
                                pen.PenWidth.SetExpression(newValue);
                            };
                            es.DropDownControl(editor);
                            pen.PenWidth.SetExpression(editor.NewValue);
                            return pen.PenWidth;
                        }
                        else
                        {
                            IAxis axis = context.Instance as IAxis;
                            if (axis != null)
                            {
                                editor = new SliderControl((int)axis.Scale.Value, 1, 500, 50);
                                editor.RefreshObject += (int newValue) =>
                                {
                                    axis.Scale.SetExpression(newValue);
                                };
                                es.DropDownControl(editor);
                                axis.Scale.SetExpression(editor.NewValue);
                                return axis.Scale;
                            }
                        }
                    }
                }
            }
            return value;
        }
    }
}